<?php

namespace app\controllers;

use Yii;
use yii\db\Query;
use yii\filters\AccessControl;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\Patient;
use app\models\SampleFrom;
use app\models\SampleDeliver;
use app\models\SamplePosition;
use app\models\SamplePositionElse;
use app\models\SampleDnaAlter;
use app\models\SampleDnaAlter4;
use app\models\SampleRnaAlter;
use app\components\PtAccessControl;
use app\components\WarningBehavior;

class SampleController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'pt_access' => [
                'class' => PtAccessControl::className(),
            ],
            'warning' => [
                'class' => WarningBehavior::className(),
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->redirect('/sample/view-from');
    }

    public function actionViewFrom()
    {
        $id = Yii::$app->request->get('id', false);
        if ($id) {
            return $this->redirect(['/from/view/', 'id' => $id]);
        }

        $dataProvider = new ActiveDataProvider([
            'query' => SampleFrom::find()->where(['pt_id' => Yii::$app->session->get('pt_id')]),
            'sort' => [
                'defaultOrder' => [
                    'crt_time' => SORT_DESC,
                ]
            ],
        ]);

        return $this->render('from', [
            'view' => 'index',
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionCreateFrom()
    {
        $pt_id = Yii::$app->session->get('pt_id');

        $model = new SampleFrom();
        $toDeliver = new SampleDeliver();
        // $total = ['dna' => $this->findDna($pt_id)->dna_total, 'rna' => $this->findRna($pt_id)->rna_total];

        $model->defaultValue();

        return $this->render('from', [
            'view' => 'create',
            'model' => $model,
            'deliver' => $toDeliver,
            // 'total' => $total,
        ]);
    }

    public function actionViewDna()
    {
        return $this->viewXna('dna');
    }

    public function actionViewRna()
    {
        return $this->viewXna('rna');
    }

    protected function viewXna($type)
    {
        $type = ($type == 'dna') ? 1 : 2;
        $pt_id = Yii::$app->session->get('pt_id');

        $dataProvider = SamplePosition::find()->where(['pt_id' => Yii::$app->session->get('pt_id'), 'xna_type' => $type])->all();

        foreach ($dataProvider as $data) {
            $data->part = SamplePositionElse::findOne(['xna_id' => $data->xna_id.'-M']);
        }

        return $this->render('xna', [
            'view' => 'index',
            'dataProvider' => $dataProvider,
            'type' => $type,
        ]);
    }

}
